﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Controls.DataVisualization;

namespace DynamicChart.UI
{
    public partial class ChartPart : UserControl, IChartPart
    {
        public ChartPart()
        {
            InitializeComponent();
        }

        public void SetChartData(ServiceRef.ChartData data)
        {
            if (data == null)
            {
                MessageBox.Show("error!");
                return;
            }

            if (data.Type == ServiceRef.ChartType.Column)
            {
                _ctlChart.Axes.Clear();
                _ctlChart.Series.Clear();

                _ctlChart.Title = data.Caption;

                Axis xAxis = new CategoryAxis
                {
                    // 设置为X轴
                    Orientation = AxisOrientation.X,
                    // 轴的标题，不显示标题就不要设置
                    //Title = "X坐标",
                    // 是否显示GridLine 默认为fasle
                    ShowGridLines = false
                };

                LinearAxis yAxis = new LinearAxis
                {
                    Orientation = AxisOrientation.Y,
                    //Title = "Y坐标",
                    ShowGridLines = false
                };
                
                if(data.XAxesMinimum!=0)
                {
                    // 区间最小值
                    yAxis.Minimum = data.XAxesMinimum;
                }
                if(data.XAxesInterval!=0)
                {
                    // 步长
                    yAxis.Interval = data.XAxesInterval;
                }
                if(data.XAxesMaximum!=0)
                {
                    // 区间最大值
                    yAxis.Maximum = data.XAxesMaximum;
                }

                _ctlChart.Axes.Add(xAxis);
                _ctlChart.Axes.Add(yAxis);

                //ColumnSeries series = new ColumnSeries();
                //series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
                //series.DependentValueBinding = new System.Windows.Data.Binding("Value");

                //_ctlChart.Palette = PaletteUtil.BuildColumnSeriesPalette(data.Items, _ctlChart.Palette);

                //series.IsSelectionEnabled = true;
                //series.SelectionChanged += new SelectionChangedEventHandler(series_SelectionChanged);

                //_ctlChart.Series.Add(series);
                //series.ItemsSource = data.Items;

                StackedColumnSeries series = new StackedColumnSeries();

                for (int seriesIndex = 0; seriesIndex < data.SeriesData.Count; seriesIndex++)
                {
                    SeriesDefinition seriesDefinition = new SeriesDefinition();
                    seriesDefinition.Title = data.SeriesData[seriesIndex].Caption;
                    seriesDefinition.IndependentValueBinding = new System.Windows.Data.Binding("Name");
                    seriesDefinition.DependentValueBinding = new System.Windows.Data.Binding("Value");
                    seriesDefinition.ItemsSource = data.SeriesData[seriesIndex].Items;
                    series.SeriesDefinitions.Add(seriesDefinition);
                }

                _ctlChart.Series.Add(series);
            }
            else if (data.Type == ServiceRef.ChartType.Pie)
            {
                _ctlChart.Axes.Clear();
                _ctlChart.Series.Clear();
                _ctlChart.Title = data.Caption;

                PieSeries series = new PieSeries();
                series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
                series.DependentValueBinding = new System.Windows.Data.Binding("Value");

                _ctlChart.Palette = PaletteUtil.BuildPieSeriesPalette(data.SeriesData[0].Items, series.Palette);

                series.IsSelectionEnabled = true;
                series.SelectionChanged+=new SelectionChangedEventHandler(series_SelectionChanged);

                _ctlChart.Series.Add(series);

                series.ItemsSource = data.SeriesData[0].Items;
            }
            else
            {
                MessageBox.Show("unknown chart type!");
                return;
            }


            _ctlMask.Visibility = System.Windows.Visibility.Collapsed;
            Storyboard1.Stop();
        }


        void series_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        public void IsBusy()
        {
            _ctlMask.Visibility = System.Windows.Visibility.Visible;
            Storyboard1.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard1.Begin();
        }
    }
}
